<template>
  <!-- 更新昵称 -->
  <div class="update-gender">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      @confirm="onConfirm"
      @cancel="onCancel"
      @change="onChange"
      :default-index="value"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "@/api/user";

export default {
  name: "UpdateGender",
  props: {
    value: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      columns: ["男", "女"],
      localGender: this.value,
    };
  },
  methods: {
    async onConfirm(value, index) {
      this.$toast({
        message: "更新中...",
        forbidClick: true,
        duration: 0,
      });
      try {
        const localGender = this.localGender;
        await updateUserProfile({
          gender: localGender,
        });
        this.$emit("input", localGender);
        this.$emit("close");
        this.$toast.success("更新成功");
      } catch (err) {
        this.$toast.fail("更新失败");
      }
    },
    // 拿到更新之后的索引
    onChange(picker, value, index) {
      this.localGender = index;
    },
    onCancel() {
      this.$emit("close");
    },
  },
};
</script>

<style></style>
